import org.apache.commons.lang.ArrayUtils
/**
 * Created by IntelliJ IDEA.
 * User: jiming
 * Date: 11-4-3
 * Time: 下午1:15
 * To change this template use File | Settings | File Templates.
 */

class PantonePair {
  def pantone;
  def red;
  def green;
  def blue;
  int nextStartIndex;
}
def getNextPair(byte[] content, int start_index) {
  if(content[start_index] == 0x00 && content[start_index+1] == 0x00 && content[start_index+2] == 0x00 && content[start_index+3] == 0x00) {
    return null;
  }
  def result = new PantonePair();
  def sb = new StringBuilder();
  int i = start_index;
  for(; content[i] != 0; i++) {
    sb.append((char)content[i])
  }
  result.pantone = sb.toString();
  result.red = 0xFF&content[i+1];
  result.green =  0xFF&content[i+2];
  result.blue =  0xFF&content[i+3];

  result.nextStartIndex = i+4;
  return result;
}


def dllName = 'C:/p2/pantone/data.dll';

def dllFile = new File(dllName);

byte[] bytes = dllFile.getBytes();

int idx = 0;
for(;;) {
  idx = ArrayUtils.indexOf(bytes, (byte)0x31, idx);
  if(idx < 0) break
  if(bytes[idx+1] == (byte)0x31 && bytes[idx+2] == (byte)0x2d) {
    break;
  }

  idx++;
}

BufferedWriter writer = new File('r:/pantoneTpx.txt').newWriter() ;

PantonePair pair = getNextPair(bytes, idx);
while (pair != null) {
  String line = pair.pantone + ', ' + pair.red + ', ' + pair.green + ', ' + pair.blue;
  // println(line);
  writer.writeLine(line)
  pair = getNextPair(bytes, pair.nextStartIndex);
}
writer.flush();

'''
SELECT pantone FROM pantone
 WHERE red > 199 -10 AND red <199 + 10
   AND green >174 - 10 AND green < 174 + 10
   AND blue >120 - 10 AND blue < 120 + 10
 ORDER BY pantone
'''